<template>
  <div>
    <!-- 蓝色添加按钮 -->
    <el-button type="primary" @click="dialogFormVisible = true"
      >添加分类</el-button
    >
    <!-- 红色批量删除按钮 -->
    <el-button type="danger">批量删除</el-button>

    <!-- 表格 -->
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="catename" label="分类名称" sortable width="180">
      </el-table-column>
      <el-table-column prop="name" label="状态" sortable width="180">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
        <!-- 写一个可以删除当前页面的按钮 -->
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >


       
        </template>
      </el-table-column>
    </el-table>
 <!-- 点击编辑打开当行数据的弹框 -->
    <el-dialog title="编辑分类" :visible.sync="dialogFormVisible2" width="30%">
      <el-form :model="form" ref="form" label-width="80px">
        <el-form-item label="上级分类">
          <el-select v-model="form.pid" placeholder="请选择上级分类">
            <el-option
              v-for="item in tableData"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="分类名称">
          <el-input v-model="form.catename"></el-input>
        </el-form-item>

        <el-form-item label="分类图片">
          <el-upload
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-success="handleSuccess"
            :on-error="handleError"
            :file-list="fileList"
            :limit="3"
            :on-exceed="handleExceed"
            :auto-upload="false"
            :list-type="'picture-card'"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog
            :visible.sync="dialogVisible"
            width="30%"
            :before-close="handleClose"
          >
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>

        <el-form-item label="分类状态">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible2 = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    


    <!-- 添加分类弹框,上级分类option,分类名称,分类图片选择器可预览,分类状态 -->
    <el-dialog title="添加分类" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" ref="form" label-width="80px">
        <el-form-item label="上级分类">
          <el-select v-model="form.pid" placeholder="请选择上级分类">
            <el-option
              v-for="item in tableData"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="分类名称">
          <el-input v-model="form.catename"></el-input>
        </el-form-item>

          <!-- 可以上传本地的图片在里面展示并且可以上传到服务器 -->
      <el-upload
        class="upload-demo"
        action="https://jsonplaceholder.typicode.com/posts/"
        :on-preview="handlePictureCardPreview" 
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        :on-success="handleSuccess" 
        :on-error="handleError"
        :file-list="fileList"
        :limit="3"
        :on-exceed="handleExceed"
        :auto-upload="false"
        :list-type="'picture-card'"
      >

        <i class="el-icon-plus"></i>
      </el-upload>


      <el-dialog
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <img width="100%" :src="dialogImageUrl" alt="" />
      </el-dialog>


      <el-form-item label="分类状态">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {



      //表格数据
      tableData: [],
      //点击显示弹框
      dialogFormVisible: false,
      //图片上传
      dialogImageUrl: "",
      dialogVisible: false,
      fileList: [],
      // 编辑
      dialogFormVisible2: false,
      // 删除
      


    
    
      //表单数据
      form: {





        pid: "",
        catename: "",
        img: "",
        status: 1,
      },
      // 删除
    
   
      // 编辑
      form3: {
        id: "",
        catename: "",
        pid: "",
        img: "",
        status: 1,
      },

      //图片路径
      imgurl: "",
    };
  },
  methods: {
    //获取分类数据
    getCategory() {
      this.$api.reqGetCates().then((res) => {
        console.log(res);
        if (res.data.meta.status === 200) {
          this.tableData = res.data.data;
        }
      });
    },

    //添加分类
    addCategory() {
      this.dialogFormVisible = false;
      console.log("添加分类");
      //添加
      this.$api.reqAddCates(this.form).then((res) => {
        console.log(res);
        if (res.data.meta.status === 201) {
          this.$message.success(res.data.meta.msg);
          this.$router.push("/category");
        } else {
          this.$message.error(res.data.meta.msg);
        }
      });
    },

    //图片上传
  handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleSuccess(res, file) {
      this.fileList.push({
        name: res.data.name,
        url: res.data.url,
      });
    },
    handleError(err, file) {
      console.log(err, file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },

    //编辑
    handleEdit(index, row) {
      this.dialogFormVisible2 = true;
      console.log(index, row);
      this.form = row;  

    },
    // 删除 


   
    // 编辑
    editCategory() {
      this.dialogFormVisible2 = false;
      console.log("编辑分类");
      //编辑
      this.$api.reqEditCates(this.form).then((res) => {
        console.log(res);
        if (res.data.meta.status === 200) {
          this.$message.success(res.data.meta.msg);
          this.$router.push("/category");
        } else {
          this.$message.error(res.data.meta.msg);
        }
      });
    },
    
  },
  created() {},
  mounted() {
    //获取分类列表
    this.$api.reqGetCates({ istree: true }).then((res) => {
      console.log(res);
      this.tableData = res.data.list;
    });
  },
  components: {},
  computed: {},
  watch: {},
};
</script>

<style lang='scss' scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
